package com.unidue.foguing.task_B;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.unidue.foguing.helper.BaseLineBase;

/**
 * this class make the preprocessing of each tweet and set the polarity based on the number of positive 
 * and negative words contained in the tweet.
 * @author foguing
 *
 */
public class BaseLine_TaskB extends BaseLineBase {
	
	private ArrayList<String> generalFeatureList = new ArrayList<String>();

	@Override
	public String getSentimentPolarity(String processedTweet) {
		String polarity = null;
		int positiveWords = 0;
		int negativeWords = 0;
		HashSet<String> positiveSetOfWords = null;
		HashSet<String> negativeSetOfWords = null;
		
		try {
			positiveSetOfWords = getSetOfPositiveWords();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		try {
			negativeSetOfWords = getSetOfNegativeWords();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		String[] array = processedTweet.split(" ");
		for(String str : array){
			if(positiveSetOfWords.contains(str.trim())){
				positiveWords++;
			}
			if(negativeSetOfWords.contains(str.trim())){
				negativeWords++;
			}
			int sum = positiveWords - negativeWords;
			if(sum > 0){
				polarity = "positive";
			}else if(sum < 0){
				polarity = "negative";
			}else{
				polarity = "neutral";
			}
			
		}
		return polarity;
	}

	/**
	 * check whether the tweet contains the extracted features or not
	 * @param tweet
	 * @return
	 */
	@SuppressWarnings("unused")
	private ArrayList<String> extractFeatures(String tweet) {
		ArrayList<String> features = new ArrayList<String>();
		HashSet<String> set = new HashSet<String>();
		set.add(tweet);
		for(String str : getGeneralFeatureList()){
			boolean tweetContainFeature = set.contains(str);
			features.add("contains(" + str + ") : " + tweetContainFeature);
		}
		return features;
	}

	/**
	 * this method read a file contains stop-words and puts the words in a hashset object
	 * @return a set of stop words
	 */
	private HashSet<String> getStopWords() throws IOException{
		HashSet<String> set = new HashSet<String>();
		String stopWordsList = "src/test/resources/test/stop-words.txt";
			File file = new File(stopWordsList);
			List<String> lines = FileUtils.readLines(file);
			if(lines == null || lines.isEmpty()){
				throw new FileNotFoundException("fail to retrieve the list of stop words. check first wether the list exist or not!!!");
			}
			for(String line : lines ){
				set.add(line.trim());
			}
		return set;
	}
	
	/**
	 * check wether the tweet contains stop-words or not. if it does not contains , then add
	 * those non-stop-words to the feature-vector list.
	 * @param tweet
	 * @return a list of non stop-words words
	 */
	@SuppressWarnings("unused")
	private ArrayList<String> getFeatureVector(String tweet){
		final List<String> featureVector = new ArrayList<String>();
		HashSet<String> hashSet = null;
		try {
			hashSet = getStopWords();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		String [] array = tweet.split(" ");
		for(String word : array){
			word = removeDuplicates(word);
			word = word.replaceAll("[^a-zA-Z ]", ""); // just remove any non-letter character for example punctuation
			if(!(hashSet.contains(word.trim()))){ // when the stop-words list not contains a word, then add the word to the featurevector
				featureVector.add(word);
				generalFeatureList.add(word);
			}
		}
		return (ArrayList<String>) featureVector;
	}
	
	/**
	 * this method return the featurelist of all tweets
	 * @return the feautureList of all documents
	 */
	public ArrayList<String> getGeneralFeatureList(){
		return generalFeatureList;
	}

}
